<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
require_once dirname(__FILE__) . '/../../../incls/php/ecomutils.class.php';

session_name(SESSION);
session_start();

$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', 0);
$instance_id = MiscUtils::getParam('inid', 0);

$sys_script = 'index';

if (isset($_COOKIE['my_cart'])) {
  $_SESSION['my_cart'] = json_decode($_COOKIE['my_cart']);
}

if (!isset($_SESSION['my_cart'])) {
  $_SESSION['my_cart'] = array();
}

$addrs = array();
if (isset($_SESSION['index' . USER])) {
  $cond_vals = new stdClass(); 
  $cond_vals->c = 't.customer_id = :v1';
  $cond_vals->v = array(':v1' => $_SESSION['index' . USER]->id);
  $addrs = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $cond_vals,  't.first_name', 'ASC', NULL, NULL, NULL)->d;
}

EComUtils::update_cart($myPdo, $_SESSION['my_cart'], isset($_SESSION['index' . USER]) ? $_SESSION['index' . USER] : NULL);

$enabled_delivery = 0;
$out_of_stock = 0;
foreach ($_SESSION['my_cart'] as $idx => $pur) {
  unset($_SESSION['stock_err' . $idx]);
  if ($pur->_stock < $pur->quantity && $pur->_enabled_stock == 1) {
    $_SESSION['stock_err' . $idx] = 'Out of stock';
    $out_of_stock = 1;
  }
  if ($pur->_enabled_delivery == 1) {
    $enabled_delivery = 1;
  }
}

$cond_vals = new stdClass(); 
$cond_vals->c = 't.status = :v1';
$cond_vals->v = array(':v1' => 1);
$deliverys = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_mthd_delivery', $cond_vals,  't.title', 'ASC', NULL, NULL, NULL)->d;

$cond_vals = new stdClass(); 
$cond_vals->c = 't.status = :v1';
$cond_vals->v = array(':v1' => 1);
$payments = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_mthd_payment', $cond_vals,  't.title', 'ASC', NULL, NULL, NULL)->d;

if (!isset($_SESSION['new_first_name']) && isset($_SESSION['index' . USER])) {
  $_SESSION['new_first_name'] = $_SESSION['index' . USER]->first_name;
}
if (!isset($_SESSION['new_last_name']) && isset($_SESSION['index' . USER])) {
  $_SESSION['new_last_name'] = $_SESSION['index' . USER]->last_name;
}
if (!isset($_SESSION['new_phone']) && isset($_SESSION['index' . USER])) {
  $_SESSION['new_phone'] = $_SESSION['index' . USER]->phone;
}
if (!isset($_SESSION['new_email']) && isset($_SESSION['index' . USER])) {
  $_SESSION['new_email'] = $_SESSION['index' . USER]->account;
}
?>
<!DOCTYPE html>
<html>
<head>
<script src="../../../libs/jquery/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../libs/json/json2.js"></script>
<script src="../../../libs/jquery_ui/js/jquery-ui-1.10.2.custom.js"></script>
<link href="jquery_ui/css/smoothness/jquery-ui-1.10.2.custom.css" rel="stylesheet">
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
}
table.list {
  border-left: 1px solid #aaa;
  border-top: 1px solid #aaa;
  font-size: 13px;
  width: 100%;
}
table.list th.sort {
  text-align: left;
  height: 28px;
  background-color: #ddd;
  padding: 0 5px; 
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  font-weight: bold;
  vertical-align: middle;
  cursor: pointer;
  color: #333;
}
table.list tr.rowodd td {
  padding: 0 5px;
  height: 25px;
  background-color: #eee;
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  word-wrap: break-word;
  vertical-align: middle;
}
table.list tr.roweven td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  word-wrap: break-word;
  vertical-align: middle;
}
table.list tr.rowodd:hover td {
  background-color: #f4f3b2;
}
table.list tr.roweven:hover td {
  background-color: #f4f3b2;
}
table.list tr.none td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
}
table.list input[type=text] {
  width: 99%;
  border: none;
}
table.form {
  background-color: #fff;
  border-top: 1px solid #aaa;
  border-left: 1px solid #aaa;
  width: 100%;
}
table.form td {
  padding: 3px 5px; 
  vertical-align: top;
}
table.form td.title {
  background-color: #ddd !important;
  border-bottom: 1px solid #aaa;
  border-right: 1px solid #aaa;
  width: 150px;
}
table.form td.content {
  padding: 0;
  border-bottom: 1px solid #aaa;
  border-right: 1px solid #aaa;
}
table.form td.content input {
  width: 98%;
  height: 20px;
  border: 0;
  padding-left: 2px;
}
table.form td.content textarea {
  width: 98%;
  height: 100px;
  border: 0;
  padding-left: 2px;
  resize: none;
  overflow: auto;
}
div.wrap {
  padding: 5px 15px 25px 15px;
  background: #eee;
  border: 1px solid #ddd;
  width: 915px;
}
div.title {
  font-weight: bold;
  font-size: 16px;
  margin-bottom: 5px;
  font-family: Lucida Sans Unicode;
}
.img {
  background:url(images/delete24.png) no-repeat;
  cursor:pointer;
  border: none;
  width: 100%;
  height: 24px;
}
.edit {
  background:url(images/edit.png) no-repeat !important;
}
.imgbtn {
  background-image: url(images/order.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 30px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
  height: 45px;
  width: 155px;
  padding-left: 40px;
  font-family: Lucida Sans Unicode;
}
.imgbtn:hover {
  background-color: #eee;
}
.cart {
  background-image: url(images/cart.png);
  background-repeat: no-repeat;
}
.ok {
  background-image: url(images/ok.png);
  width: 80px;
  height: 30px;
  padding-left: 30px;
}
.cancel {
  background-image: url(images/cancel.png);
  margin-left: 5px;
  width: 80px;
  height: 30px;
  padding-left: 30px;
}
form {
  padding: 0;
  margin: 0;
}
div.lo {
  text-align: right;
}
div.border {
  border: 1px solid #ddd;
  background: #fff;
  padding: 5px 10px;
  width: 200px;
  margin: 5px 0 0 auto;
}
div.total {
  text-align: right;
  font-size: 14px;
  font-weight: bold;
}
div.subtotal {
  text-align: right;
  font-size: 12px;
  font-weight: bold;
}
div.section {
  border-bottom: 1px dotted #666;
  word-wrap: break-word;
  padding: 10px 0;
}
div.content {
  text-align: center;
  padding: 10px 0;
}
.comment {
  width: 100%;
  border: 1px solid #ddd;
  resize: none;
  height: 150px;
  overflow-y: auto;
}
input.total {
  text-align: right;
  background: #fff;
  border: none;
  font-weight: bold;
  width: 60px;
}
</style>
</head>
<body>
<?php if ($out_of_stock == 1 || count($_SESSION['my_cart']) == 0) { ?>
<div>Redirecting...</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
  $(function() {
    parent.location.href = 'http://<?php echo URL . '?i=' . $page_id . '&script=index_cart'; ?>';
  });
</script>
<?php } else { ?>
<div class="wrap">
  <div class="section">
    <div class="title">Finalise Order</div>
  </div>
  <?php if ($enabled_delivery == 1) { ?>
  <div class="section">
    <?php if (!isset($_SESSION['index' . USER])) { ?>
    <b>Post Address</b>
    <table class="form" cellpadding=0 cellspacing=0>
      <tr>
        <td class="title">First Name</td>
        <td class="content"><input id="new_first_name" name="new_first_name" type="text" <?php if (isset($_SESSION['new_first_name'])) echo 'value="' . $_SESSION['new_first_name'] . '"'; ?>></input><?php if (isset($_SESSION['new_first_name_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_first_name_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Last Name</td>
        <td class="content"><input id="new_last_name" name="new_last_name" type="text" <?php if (isset($_SESSION['new_last_name'])) echo 'value="' . $_SESSION['new_last_name'] . '"'; ?>></input><?php if (isset($_SESSION['new_last_name_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_last_name_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Address1</td>
        <td class="content"><input id="new_address1" name="new_address1" type="text" <?php if (isset($_SESSION['new_address1'])) echo 'value="' . $_SESSION['new_address1'] . '"'; ?>></input><?php if (isset($_SESSION['new_address1_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_address1_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Address2 (optional)</td>
        <td class="content"><input id="new_address2" name="new_address2" type="text" <?php if (isset($_SESSION['new_address2'])) echo 'value="' . $_SESSION['new_address2'] . '"'; ?>></input><?php if (isset($_SESSION['new_address2_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_address2_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">City</td>
        <td class="content"><input id="new_city" name="new_city" type="text" <?php if (isset($_SESSION['new_city'])) echo 'value="' . $_SESSION['new_city'] . '"'; ?>></input><?php if (isset($_SESSION['new_city_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_city_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Country</td>
        <td class="content"><input id="new_country" name="new_country" type="text" <?php if (isset($_SESSION['new_country'])) echo 'value="' . $_SESSION['new_country'] . '"'; ?>></input><?php if (isset($_SESSION['new_country_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_country_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Postal Code</td>
        <td class="content"><input id="new_zip" name="new_zip" type="text" <?php if (isset($_SESSION['new_zip'])) echo 'value="' . $_SESSION['new_zip'] . '"'; ?>></input><?php if (isset($_SESSION['new_zip_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_zip_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Phone</td>
        <td class="content"><input id="new_phone" name="new_phone" type="text" <?php if (isset($_SESSION['new_phone'])) echo 'value="' . $_SESSION['new_phone'] . '"'; ?>></input><?php if (isset($_SESSION['new_phone_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_phone_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Email</td>
        <td class="content"><input id="new_email" name="new_email" type="text" <?php if (isset($_SESSION['new_email'])) echo 'value="' . $_SESSION['new_email'] . '"'; ?>></input><?php if (isset($_SESSION['new_email_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_email_err']; ?></div><?php } ?></td>
      </tr>
    </table>
    <?php } else { ?>
    <b>Post Addresses</b>
    <table class="list" cellpadding=0 cellspacing=0>
      <tr>
        <th class="sort" width="45px">&nbsp;</th>
        <th class="sort" width="100px">First Name</th>
        <th class="sort" width="100px">Last Name</th>
        <th class="sort">Address</th>
        <th class="sort" width="60px">City</th>
        <th class="sort" width="80px">Phone</th>
        <th class="sort" width="80px">Email</th>
        <th class="sort" width="20px" style="border-right: none;padding-right: 0;">&nbsp;</th>
        <th class="sort" width="30px" style="padding-left: 0;">&nbsp;</th>
      </tr>
      <?php foreach ($addrs as $idx => &$addr) { ?>
      <tr class="<?php echo (($idx % 2 == 0) ? 'roweven' : 'rowodd'); ?>">
        <td><input type="radio" name="addr" <?php if ($addr->is_default == 1) echo 'checked="checked"'; ?> value="<?php echo $idx; ?>" onclick="set_new_addr('none');"></input></td>
        <td><?php echo MiscUtils::getShortName($addr->first_name, 20); ?>&nbsp;</td>
        <td><?php echo MiscUtils::getShortName($addr->last_name, 20); ?>&nbsp;</td>
        <td><?php echo MiscUtils::getShortName($addr->address1, 50); ?>&nbsp;</td>
        <td><?php echo MiscUtils::getShortName($addr->city, 20); ?>&nbsp;</td>
        <td><?php echo MiscUtils::getShortName($addr->phone, 20); ?>&nbsp;</td>
        <td><?php echo MiscUtils::getShortName($addr->email, 20); ?>&nbsp;</td>
        <td style="border-right: none;padding-right: 0;">
          <input type='submit' value="" class="img edit" onclick="edit_addr('<?php echo $idx; ?>');"></input>
        </td>
        <td style="padding-left: 0;">
          <form id="del_addr<?php echo $idx; ?>" action="<?php echo basename(__FILE__, '.php'); ?>.server.php" method="post">
            <input name="action" value="remove_addr" style="display: none;"></input>
            <input name="id" value="<?php echo $addr->id; ?>" style="display: none;"></input>
            <input name="is_default" value="<?php echo $addr->is_default; ?>" style="display: none;"></input>
            <input name="customer_id" value="<?php echo $addr->customer_id; ?>" style="display: none;"></input>
            <input name="to" value="<?php echo MiscUtils::curPageURL(); ?>" style="display: none;"></input>
            <input type='submit' value="" class="img"></input>
          </form>
          <script>
          $('#del_addr<?php echo $idx; ?>').submit(function() {
            return window.confirm('Are You Sure?');
          });
          </script>
        </td>
      </tr>
      <?php } ?>
      <tr class="<?php echo ((($idx + 1) % 2 == 0) ? 'roweven' : 'rowodd'); ?>">
        <td><input type="radio" name="addr" <?php if (count($addrs) == 0 || isset($_SESSION['select_new_addr'])) echo 'checked="checked"'; ?> value="<?php echo $idx + 1; ?>" onclick="set_new_addr('table-row');"></input></td>
        <td colspan=10>New Address</td>
      </tr>
      <tr id="new_addr" <?php if (count($addrs) > 0 && !isset($_SESSION['select_new_addr'])) echo 'style="display: none;"'; ?> class="roweven">
        <td style="border-right: none;">&nbsp;</td>
        <td colspan=10 style="padding: 5px 60px 15px 0;">
          <table class="form" cellpadding=0 cellspacing=0>
            <tr>
              <td class="title">First Name</td>
              <td class="content"><input id="new_first_name" name="new_first_name" type="text" <?php if (isset($_SESSION['new_first_name'])) echo 'value="' . $_SESSION['new_first_name'] . '"'; ?>></input><?php if (isset($_SESSION['new_first_name_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_first_name_err']; ?></div><?php } ?></td>
            </tr>
            <tr>
              <td class="title">Last Name</td>
              <td class="content"><input id="new_last_name" name="new_last_name" type="text" <?php if (isset($_SESSION['new_last_name'])) echo 'value="' . $_SESSION['new_last_name'] . '"'; ?>></input><?php if (isset($_SESSION['new_last_name_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_last_name_err']; ?></div><?php } ?></td>
            </tr>
            <tr>
              <td class="title">Address1</td>
              <td class="content"><input id="new_address1" name="new_address1" type="text" <?php if (isset($_SESSION['new_address1'])) echo 'value="' . $_SESSION['new_address1'] . '"'; ?>></input><?php if (isset($_SESSION['new_address1_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_address1_err']; ?></div><?php } ?></td>
            </tr>
            <tr>
              <td class="title">Address2 (optional)</td>
              <td class="content"><input id="new_address2" name="new_address2" type="text" <?php if (isset($_SESSION['new_address2'])) echo 'value="' . $_SESSION['new_address2'] . '"'; ?>></input><?php if (isset($_SESSION['new_address2_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_address2_err']; ?></div><?php } ?></td>
            </tr>
            <tr>
              <td class="title">City</td>
              <td class="content"><input id="new_city" name="new_city" type="text" <?php if (isset($_SESSION['new_city'])) echo 'value="' . $_SESSION['new_city'] . '"'; ?>></input><?php if (isset($_SESSION['new_city_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_city_err']; ?></div><?php } ?></td>
            </tr>
            <tr>
              <td class="title">Country</td>
              <td class="content"><input id="new_country" name="new_country" type="text" <?php if (isset($_SESSION['new_country'])) echo 'value="' . $_SESSION['new_country'] . '"'; ?>></input><?php if (isset($_SESSION['new_country_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_country_err']; ?></div><?php } ?></td>
            </tr>
            <tr>
              <td class="title">Postal Code</td>
              <td class="content"><input id="new_zip" name="new_zip" type="text" <?php if (isset($_SESSION['new_zip'])) echo 'value="' . $_SESSION['new_zip'] . '"'; ?>></input><?php if (isset($_SESSION['new_zip_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_zip_err']; ?></div><?php } ?></td>
            </tr>
            <tr>
              <td class="title">Phone</td>
              <td class="content"><input id="new_phone" name="new_phone" type="text" <?php if (isset($_SESSION['new_phone'])) echo 'value="' . $_SESSION['new_phone'] . '"'; ?>></input><?php if (isset($_SESSION['new_phone_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_phone_err']; ?></div><?php } ?></td>
            </tr>
            <tr>
              <td class="title">Email</td>
              <td class="content"><input id="new_email" name="new_email" type="text" <?php if (isset($_SESSION['new_email'])) echo 'value="' . $_SESSION['new_email'] . '"'; ?>></input><?php if (isset($_SESSION['new_email_err'])) { ?><div style="color:red;"><?php echo $_SESSION['new_email_err']; ?></div><?php } ?></td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
    <?php } ?>
    <div>&nbsp;</div>
    <b>Delivery Method</b>
    <table class="list" cellpadding=0 cellspacing=0>
      <tr>
        <th class="sort" style="width: 45px;">&nbsp;</th>
        <th class="sort">Courier Company</th>
        <th class="sort" style="width: 150px;">Price</th>
      </tr>
      <?php foreach ($deliverys as $idx => $de) { ?>
      <tr class="<?php echo (($idx % 2 == 0) ? 'roweven' : 'rowodd'); ?>">
        <td><input type="radio" name="deliver" <?php if ($idx == 0) echo 'checked="checked"'; ?> onclick="update_total();" value="<?php echo $idx; ?>"></input></td>
        <td><?php echo $de->title; ?></td>
        <td>$<?php echo $de->price; ?></input></td>
      </tr>
      <?php } ?>
    </table>
  </div>
  <?php } ?>
  <div class="section">
    <b>Payment Method</b>
    <table class="list" cellpadding=0 cellspacing=0>
      <tr>
        <th class="sort" style="width: 45px;">&nbsp;</th>
        <th class="sort">Pay by</th>
      </tr>
      <?php foreach ($payments as $idx => $pm) { ?>
      <tr class="<?php echo (($idx % 2 == 0) ? 'roweven' : 'rowodd'); ?>">
        <td><input type="radio" name="pay" <?php if ($idx == 0) echo 'checked="checked"'; ?> value="<?php echo $idx; ?>"></input></td>
        <td><?php echo $pm->title; ?></td>
      </tr>
      <?php } ?>
    </table>
  </div>
  <div>&nbsp;</div>
  <b>Shopping Cart</b>
  <table id="cart" class="list" cellpadding=0 cellspacing=0>
    <tr>
      <th class="sort">ITEM</th>
      <th class="sort" style="width: 80px;">QTY</th>
      <th class="sort" style="width: 40px;">STOCK</th>
      <th class="sort" style="width: 60px;">EACH</th>
      <th class="sort" style="width: 60px;">TOTAL</th>
    </tr>
    <?php foreach ($_SESSION['my_cart'] as $idx => $pur) { ?>
    <tr class="<?php echo (($idx % 2 == 0) ? 'roweven' : 'rowodd'); ?>">
      <td title="<?php echo $pur->name . (($pur->attr_opt_code != '') ? '(' . $pur->attr_opt_code . ')' : ''); ?>"><?php echo MiscUtils::getShortName($pur->name . (($pur->attr_opt_code != '') ? '(' . $pur->attr_opt_code . ')' : ''), 50); ?></td>
      <td>
        <?php echo $pur->quantity; ?>
        <?php if (isset($_SESSION['stock_err' . $idx])) { ?>
        <div style="color: red;"><?php echo $_SESSION['stock_err' . $idx]; ?></div>
        <?php } ?>
      </td>
      <td><?php echo $pur->_stock; ?></td>
      <td>$<?php echo $pur->price; ?></td>
      <td id="total<?php echo $idx; ?>">$<?php echo sprintf("%01.2f", $pur->total); ?></td>
    </tr>
    <?php } ?>
    <?php if (count($_SESSION['my_cart']) == 0) { ?>
    <tr class="none"><td colspan=6>None</td></tr>
    <?php } ?>
  </table>
  <div>&nbsp;</div>
  <b>Comment</b><br>
  <textarea id="comment" class="comment"></textarea>
  <div class="right">
    <div class="border">
      <div class="total">Total: $<input class="total" id="cart_total"></input></div>
      <div class="subtotal">Delivery Subtotal: $<span id="delivery_total"></span></div>
    </div>
  </div>
</div>
<div>&nbsp;</div>
<table style="width: 100%;" cellspacing=0 cellpadding=0>
  <tr>
    <td style="width: 155px; padding-left: 5px;"><input type='submit' value="Back to Cart" class="imgbtn cart" onclick="parent.location.href='http://<?php echo URL . '?i=' . $page_id . '&script=index_cart'; ?>'"></input></td>
    <td style="text-align: right;">
      <form id="next" action="<?php echo basename(__FILE__, '.php'); ?>.server.php" method="post">
        <input name="action" value="place_order" style="display: none;"></input>
        <input name="to" value="http://<?php echo URL . '?i=' . $page_id . '&script=index_paynow' ?>" style="display: none;"></input>
        <input name="from" value="<?php echo MiscUtils::curPageURL(); ?>" style="display: none;"></input>
        <input id="my_order" name="my_order" value="" style="display: none;"></input>
        <input type='submit' value="Place Order" class="imgbtn" onclick="document.getElementById('po_load').style.display='table-cell';"></input>
      </form>
    </td>
    <td id="po_load" style="width: 30px; display: none;"><img src="images/loading.gif"></img></td>
  </tr>
</table>
<div>&nbsp;</div>
<!-- Popup box for editing address -->
<div id="dia_addr" style="display: none;">
  <form action="<?php echo basename(__FILE__, '.php'); ?>.server.php" method="post">
    <input name="action" value="update_addr" style="display: none;"></input>
    <input name="to" value="<?php echo MiscUtils::curPageURL(); ?>" style="display: none;"></input>
    <input id="update_idx" name="update_idx" <?php if (isset($_SESSION['update_addr_idx'])) echo 'value="' . $_SESSION['update_addr_idx'] . '"'; ?> style="display: none;"></input>
    <input id="update_id" name="update_id" <?php if (isset($_SESSION['update_addr_id'])) echo 'value="' . $_SESSION['update_addr_id'] . '"'; ?> style="display: none;"></input>
    <table class="form" cellpadding=0 cellspacing=0>
      <tr>
        <td class="title">First Name</td>
        <td class="content"><input id="update_first_name" name="update_first_name" type="text" <?php if (isset($_SESSION['update_first_name'])) echo 'value="' . $_SESSION['update_first_name'] . '"'; ?>></input><?php if (isset($_SESSION['update_first_name_err'])) { ?><div style="color:red;"><?php echo $_SESSION['update_first_name_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Last Name</td>
        <td class="content"><input id="update_last_name" name="update_last_name" type="text" <?php if (isset($_SESSION['update_last_name'])) echo 'value="' . $_SESSION['update_last_name'] . '"'; ?>></input><?php if (isset($_SESSION['update_last_name_err'])) { ?><div style="color:red;"><?php echo $_SESSION['update_last_name_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Address1</td>
        <td class="content"><input id="update_address1" name="update_address1" type="text" <?php if (isset($_SESSION['update_address1'])) echo 'value="' . $_SESSION['update_address1'] . '"'; ?>></input><?php if (isset($_SESSION['update_address1_err'])) { ?><div style="color:red;"><?php echo $_SESSION['update_address1_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Address2 (optional)</td>
        <td class="content"><input id="update_address2" name="update_address2" type="text" <?php if (isset($_SESSION['update_address2'])) echo 'value="' . $_SESSION['update_address2'] . '"'; ?>></input><?php if (isset($_SESSION['update_address2_err'])) { ?><div style="color:red;"><?php echo $_SESSION['update_address2_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">City</td>
        <td class="content"><input id="update_city" name="update_city" type="text" <?php if (isset($_SESSION['update_city'])) echo 'value="' . $_SESSION['update_city'] . '"'; ?>></input><?php if (isset($_SESSION['update_city_err'])) { ?><div style="color:red;"><?php echo $_SESSION['update_city_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Country</td>
        <td class="content"><input id="update_country" name="update_country" type="text" <?php if (isset($_SESSION['update_country'])) echo 'value="' . $_SESSION['update_country'] . '"'; ?>></input><?php if (isset($_SESSION['update_country_err'])) { ?><div style="color:red;"><?php echo $_SESSION['update_country_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Postal Code</td>
        <td class="content"><input id="update_zip" name="update_zip" type="text" <?php if (isset($_SESSION['update_zip'])) echo 'value="' . $_SESSION['update_zip'] . '"'; ?>></input><?php if (isset($_SESSION['update_zip_err'])) { ?><div style="color:red;"><?php echo $_SESSION['update_zip_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Phone</td>
        <td class="content"><input id="update_phone" name="update_phone" type="text" <?php if (isset($_SESSION['update_phone'])) echo 'value="' . $_SESSION['update_phone'] . '"'; ?>></input><?php if (isset($_SESSION['update_phone_err'])) { ?><div style="color:red;"><?php echo $_SESSION['update_phone_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Email</td>
        <td class="content"><input id="update_email" name="update_email" type="text" <?php if (isset($_SESSION['update_email'])) echo 'value="' . $_SESSION['update_email'] . '"'; ?>></input><?php if (isset($_SESSION['update_email_err'])) { ?><div style="color:red;"><?php echo $_SESSION['update_email_err']; ?></div><?php } ?></td>
      </tr>
    </table>
    <div style="text-align: center; margin-top: 5px;">
      <input class="imgbtn ok" type="submit" value="OK"></input>
      <input class="imgbtn cancel" type="submit" value="Cancel" onclick="event.returnValue = false;if(event.preventDefault) event.preventDefault();$('#dia_addr').dialog('close');"></input>
    </div>
  </form>
</div>
<script type="text/javascript">
  var _cart = JSON.parse('<?php echo addslashes(json_encode($_SESSION['my_cart'])); ?>');
  var _addrs = JSON.parse('<?php echo addslashes(json_encode($addrs)); ?>');
  var _deliverys = JSON.parse('<?php echo addslashes(json_encode($deliverys)); ?>');
  var _payments = JSON.parse('<?php echo addslashes(json_encode($payments)); ?>');

  $(function(){
    <?php if (isset($_SESSION['select_new_addr'])) { ?>
    parent.window.scrollTo(0, 0);
    <?php } ?>
    
    <?php if (isset($_SESSION['update_addr_idx'])) { ?>
    $('#dia_addr').dialog({
      title: 'Edit Address',
      width: 700
    });
    <?php } ?>
    
    parent.document.title = parent.document.title.replace('Product', 'Checkout')
    update_total();
    $('#next').submit(function() {
      var p = _payments[$('input[name=pay]:checked').val()];
      var d = _deliverys[$('input[name=deliver]:checked').val()];
      var obj = {
        date: '0000-00-00', //php config
        date_deliver: '0000-00-00', //php config
        customer_id: 0, //js config
        order_number: 0, //php config
        is_paid: 0,
        is_delivered: 0,
        is_cancel: 0,
        total: $('#cart_total')[0].value,
        comment: $('#comment')[0].value,
        payment_id: p.id,
        payment_name: p.title,
        delivery_id: d.id,
        delivery_name: d.title,
        delivery_subtotal: d.price
      };
      <?php if (!isset($_SESSION['index' . USER])) { ?>
      obj.first_name = $('#new_first_name')[0].value;
      obj.last_name = $('#new_last_name')[0].value;
      obj.address1 = $('#new_address1')[0].value;
      obj.address2 = $('#new_address2')[0].value;
      obj.city = $('#new_city')[0].value;
      obj.country = $('#new_country')[0].value;
      obj.zip = $('#new_zip')[0].value;
      obj.phone = $('#new_phone')[0].value;
      obj.email = $('#new_email')[0].value;
      obj._n = 1;
      <?php } else { ?>
      obj.customer_id = '<?php echo $_SESSION['index' . USER]->id; ?>';
      if ($('input[name=addr]:checked').val() >= _addrs.length) {
        obj.first_name = $('#new_first_name')[0].value;
        obj.last_name = $('#new_last_name')[0].value;
        obj.address1 = $('#new_address1')[0].value;
        obj.address2 = $('#new_address2')[0].value;
        obj.city = $('#new_city')[0].value;
        obj.country = $('#new_country')[0].value;
        obj.zip = $('#new_zip')[0].value;
        obj.phone = $('#new_phone')[0].value;
        obj.email = $('#new_email')[0].value;
        obj._n = 1;
      } else {
        var a = _addrs[$('input[name=addr]:checked').val()];
        obj.first_name = a.first_name;
        obj.last_name = a.last_name;
        obj.address1 = a.address1;
        obj.address2 = a.address2;
        obj.city = a.city;
        obj.country = a.country;
        obj.zip = a.zip;
        obj.phone = a.phone;
        obj.email = a.email;
      }
      <?php } ?>
      $('#my_order')[0].value = JSON.stringify(obj);
    });
  });

  function update_total() {
    var d = _deliverys[$('input[name=deliver]:checked').val()];
    var total = 0;
    for (var idx in _cart) {
      var obj = _cart[idx];
      total += parseFloat(obj.total, 2);
    }
    if (d) {
      total += parseFloat(d.price, 2);
    }
    $('#cart_total')[0].value = total.toFixed(2);
    MyUtils.removeChildElements($('#delivery_total')[0]);
    if (d) {
      $('#delivery_total')[0].appendChild(document.createTextNode(parseFloat(d.price, 2).toFixed(2)));
    } else {
      $('#delivery_total')[0].appendChild(document.createTextNode('0.00'));
    }
  }

  function set_new_addr(dis_val) {
    $('#new_addr')[0].style.display = dis_val;
    parent.updateHeight_<?php echo $instance_id; ?>();
  }

  function edit_addr(idx) {
    var addr = _addrs[idx];
    $('#update_idx')[0].value = idx;
    $('#update_id')[0].value = addr.id;
    $('#update_first_name')[0].value = addr.first_name;
    $('#update_last_name')[0].value = addr.last_name;
    $('#update_address1')[0].value = addr.address1;
    $('#update_address2')[0].value = addr.address2;
    $('#update_city')[0].value = addr.city;
    $('#update_country')[0].value = addr.country;
    $('#update_zip')[0].value = addr.zip;
    $('#update_phone')[0].value = addr.phone;
    $('#update_email')[0].value = addr.email;
    $('#dia_addr').dialog({
      title: 'Edit Address',
      width: 700
    });
  }
</script>
<?php } ?>
</body>
</html>